/**
 * Copyright (c) 2020 kedacom
 * OpenATC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 **/
export default class Svgmethods {
  clickOpen (call) {
    const file = document.getElementById('importimg').files[0]
    const filetype = file.type
    const reader = new FileReader()
    const filesize = file.size
    if (filetype !== 'image/svg+xml' && filetype !== 'image/png' && filetype !== 'image/jpeg') {
      call('error', 'type')
      return
    }
    if (filesize > 1024 * 1024 * 10) { // 10M换算字节
      call('error', 'size')
      return
    }
    if (filetype === 'image/svg+xml') {
      reader.readAsText(file, 'UTF-8')
      reader.addEventListener('load', async (readerEvent) => {
        const content = readerEvent.target.result
        call('vectorgraph', content)
      })
    }
    if (filetype === 'image/png' || filetype === 'image/jpeg') {
      reader.readAsDataURL(file)
      reader.addEventListener('load', async (readerEvent) => {
        // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
        const _base64 = reader.result
        const content = _base64
        call('picture', content)
      })
    }
  }
}
